﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace AdvancedMap
{
    class ConfigClass
    {
        public double scale = 1;
        public bool opacity = 1;
        public bool showPlayers = true;
        public bool showFriends = true;
        public bool showEnemies = true;
        public string FilterPlayers = "";
        public Regex rFilterPlayers;
        public bool showNPCs = true;
        public string FilterNPCs = "";
        public Regex rFilterNPCs;
        public bool showMonsters = true;
        public Int16 MonsterMinLvl = -4;
        public Int16 MonsterMaxLvl = 1;
        public string FilterMonsters = "";
        public Regex rFilterMonsters;
        public bool showHerbs = true;
        public string FilterHerbs = "";
        public Regex rFilterHerbs;
        public bool showMinerals = true;
        public string FilterMinerals = "";
        public Regex rFilterMinerals;
        public bool showTreasures = true;
        public string FilterTreasures="";
        public Regex rFilterTreasures;
        public bool showWaypoints = true;
        public bool showMisc = true;
        public string FilterMisc ="";
        public Regex rFilterMisc;
        public bool loggingStatus = true;
        public bool onTop = true;
        public int clientX = 100;
        public int clientY = 100;
        public int clientWidth = 300;
        public int clientHeight = 300;

        private static string targetFile = Directory.GetCurrentDirectory() + "\\Classes\\Maps\\AdMap.ini";

    }
            // Finds value to load from file
        private string FindValue(string file, string key)
        {
            try
            {
                StreamReader input = new StreamReader(file);
                string strIn;

                while ((strIn = input.ReadLine()) != null)
                {
                    if (strIn.Contains(key))
                    {
                        strIn = strIn.Replace(key + "=", "");
                        return strIn;
                    }
                }

                input.Close();
            }
            catch
            {
                Log("Error in FindValue()");
            }
            return "";
        }

        //private string Zone

        // Load a value from file
        private void LoadFromFile(string file)
        {
            Log("Loading file...");
            try
            {
                if (File.Exists(file))
                {
                    showPlayers = Convert.ToBoolean(FindValue(file, "ShowPlayers"));
                    showFriends = Convert.ToBoolean(FindValue(file, "ShowFriends"));
                    showEnemies = Convert.ToBoolean(FindValue(file, "ShowEnemies"));
                    
                    showNPCs = Convert.ToBoolean(FindValue(file, "ShowNPCs"));
                    showMonsters = Convert.ToBoolean(FindValue(file, "ShowMonsters"));
                    showHerbs = Convert.ToBoolean(FindValue(file, "ShowHerbs"));
                    showMinerals = Convert.ToBoolean(FindValue(file, "ShowMinerals"));
                    showTreasures = Convert.ToBoolean(FindValue(file, "ShowTreasures"));
                    showWaypoints = Convert.ToBoolean(FindValue(file, "ShowWaypoints"));
                    showMisc = Convert.ToBoolean(FindValue(file, "ShowMisc"));
                    loggingStatus = Convert.ToBoolean(FindValue(file, "LoggingStatus"));

                    onTop = Convert.ToBoolean(FindValue(file, "AlwaysOnTop"));

                    clientX = Convert.ToInt32(FindValue(file, "ClientX"));
                    clientY = Convert.ToInt32(FindValue(file, "ClientY"));
                    clientWidth = Convert.ToInt32(FindValue(file, "ClientWidth"));
                    clientHeight = Convert.ToInt32(FindValue(file, "ClientHeight"));

                    opacity = Convert.ToDouble(FindValue(file, "Opacity"));
                    scale = Convert.ToDouble(FindValue(file, "Scale"));
                    //mapZone = Convert.ToString(FindValue(file, "Zone"));
                    mapZone = GContext.Main.WorldMap;
                }
                else
                    Log(file + " doesn't exist. Loading defaults.");
            }
            catch
            {
                Log("Error in LoadFromFile() - possible bad format");
            }
        }

        // Save to file
        private void SaveToFile(string file)
        {
            Log("Saving to file...");
            try
            {
                TextWriter output = new StreamWriter(file);

                output.WriteLine("ShowPlayers=" + Convert.ToString(showPlayers));
                output.WriteLine("ShowNPCs=" + Convert.ToString(showNPCs));
                output.WriteLine("ShowMonsters=" + Convert.ToString(showMonsters));
                output.WriteLine("ShowHerbs=" + Convert.ToString(showHerbs));
                output.WriteLine("ShowMinerals=" + Convert.ToString(showMinerals));
                output.WriteLine("ShowTreasures=" + Convert.ToString(showTreasures));
                output.WriteLine("ShowWaypoints=" + Convert.ToString(showWaypoints));
                output.WriteLine("ShowMisc=" + Convert.ToString(showMisc));
                output.WriteLine("LoggingStatus=" + Convert.ToString(loggingStatus));

                output.WriteLine("AlwaysOnTop=" + Convert.ToString(onTop));

                output.WriteLine("ClientX=" + Convert.ToString(advMap.Location.X));
                output.WriteLine("ClientY=" + Convert.ToString(advMap.Location.Y));
                output.WriteLine("ClientWidth=" + Convert.ToString(advMap.ClientSize.Width));
                output.WriteLine("ClientHeight=" + Convert.ToString(advMap.ClientSize.Height));

                output.WriteLine("Opacity=" + Convert.ToString(advMap.Opacity));
                output.WriteLine("Scale=" + Convert.ToString(scale));
                output.WriteLine("Zone=" + GContext.Main.WorldMap);

                output.Close();
            }
            catch
            {
                Log("Error in SaveToFile()");
            }

        }

        // Update map settings after loading
        private void UpdateAfterLoad()
        {
            Log("Setting data...");
            try
            {
                playersToolStripMenuItem.Checked = showPlayers;
                nPCsToolStripMenuItem.Checked = showNPCs;
                monsterToolStripMenuItem.Checked = showMonsters;
                herbsToolStripMenuItem.Checked = showHerbs;
                mineralsToolStripMenuItem.Checked = showMinerals;
                treasuresToolStripMenuItem.Checked = showTreasures;
                waypointsToolStripMenuItem.Checked = showWaypoints;
                miscToolStripMenuItem.Checked = showMisc;
                loggingToolStripMenuItem.Checked = loggingStatus;

                this.TopMost = onTop;
                alwaysOnTopToolStripMenuItem.Checked = onTop;

                this.ClientSize = new System.Drawing.Size(clientWidth, clientHeight);
                this.SetDesktopLocation(clientX, clientY);
                //this.mapSelect.Text = mapZone;

                this.Opacity = opacity;
            }
            catch
            {
                Log("Error in UpdateAfterLoad()");
            }
        }
}
